package com.hqyj.ssm.service.impl;/*
 * ClassName  UserServiceImpl
 * @Author： AJA
 * @Description ：TODO
 *
 * @Date  2022/1/5 13:59
 * @Version 1.0
 **/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.ssm.mapper.UserMapper;
import com.hqyj.ssm.pojo.User;
import com.hqyj.ssm.service.UserService;
import com.hqyj.ssm.utils.MD5Utils;
import com.hqyj.ssm.utils.ResultInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service//注册在spring容器中
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultInfo login(String username, String password) {
        //使用shiro进行登录
        //1.获取主体subject
        Subject subject = SecurityUtils.getSubject();
        // 2、通过subject方法isAuthenticated()判断当前用户是否登录了
        if(!subject.isAuthenticated()) {
            // 未登陆操作
            // 3、获取token
            AuthenticationToken token = new UsernamePasswordToken(username, password);
            // 4、执行shiro中的login
            try {
                subject.login(token);// 会抛出异常，可以通过捕获异常获取登录失败的原因
            } catch (UnknownAccountException unknownAccountException) {
                return new ResultInfo("-1","账号不存在");
            }catch (IncorrectCredentialsException incorrectCredentialsException){
                return new ResultInfo("-2","密码错误");
            }catch (AuthenticationException authenticationException){
                return new ResultInfo("-3","非法参数");
            }catch (Exception e){
                return new ResultInfo("-4","其他问题");
            }
        }
        // 已经认证过了
        return new ResultInfo("200","登录成功",SecurityUtils.getSubject().getSession().getAttribute("loginUser"));
    }

    @Override
    public ResultInfo selectAll(int pageNum,int pageSize,String username) {
        //使用pageHelper进行分页
        //1.调用pageHelper中的startPage方法，开启分页
        PageHelper.startPage(pageNum,pageSize);
        //2.执行查询语句
        List<User> userList = userMapper.selectAll(username);
        //3.把查询出来的数据封装在PageInfo对象中
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return new ResultInfo("200","查询全部用户",pageInfo);
    }

    @Override
    public ResultInfo insertUser(User user) {
        if(user != null){
            user.setPassword(MD5Utils.md5(user.getUsername(),user.getPassword()));
            int result = userMapper.insertUser(user);
            if(result>0){
                return new ResultInfo("200","增加成功",result);
            }
            return new ResultInfo("500","增加失败",result);
        }
        return new ResultInfo("500","参数错误");
    }

    @Override
    public ResultInfo deleteUserByUsername(String username) {
        int result = userMapper.deleteUserByUsername(username);
        if(result>0){
            return new ResultInfo("200","删除成功",result);
        }
        return new ResultInfo("500","删除失败",result);
    }

    @Override
    public ResultInfo updateUserByUsername(User user) {
        if(user != null){
            user.setPassword(MD5Utils.md5(user.getUsername(),user.getPassword()));
            int result = userMapper.updateUserByUsername(user);
            if(result>0){
                return new ResultInfo("200","修改成功",result);
            }
            return new ResultInfo("500","修改失败",result);
        }
        return new ResultInfo("500","修改失败",0);
    }

    @Override
    public ResultInfo selectUser() {

        return new ResultInfo("200","查询成功");
    }

    @Override
    public ResultInfo checkUsername(String username) {
        User user = userMapper.checkUsername(username);
        //System.out.println(user);
        if(user != null){
            return new ResultInfo("500","用户存在");
        }else {
            return new ResultInfo("200","用户不存在");
        }
    }
}
















